*Replication for: TEELE (2023) The Political Geography of the Gender Gap.
*Generates figures 1 and 2 and various facts cited in the text


*cd "YOUR DIRECTORY HERE"

********************************************************
*	1. Calculate fact cited in paper. 
*	2. Generate Figure 1a
*  	3. Generate Figure 1b 
*	4. Generate Figure 1c
*	5. Generate Figure 1d
********************************************************

			 
*************************************************
*1. Generate fact cited in paper
*************************************************
		
		use "Sweden 1911-1944_panel.dta", clear

		sum Elect if year==1921 & Elect!=0 /*fact cited in paper, average municipality has 1259.73 electors, smallest 53, largest 116078*/ 
		
		
************************************************************************
*2: Figure 1a: Graph of swedish party total percentages of support 1911-1944
************************************************************************
		
		use "Sweden 1911-1944_panel.dta", clear
		
		*collapse data by year
			collapse (sum) Valid VT VTmen VTwomen Elect Electmen Electwomen SOC LIB CON OTH COM AGR Fris, by(year)

		*Create national averages
			foreach var of varlist CON LIB SOC COM AGR OTH Fris {
			gen `var'_pct = round((`var'/ Valid )*100, 0.01)
			}

		sort year
		
		# delimit ; 
		twoway 
			(connected SOC_pct year, lcolor(cranberry) mlabcolor(cranberry) msym(none) mlabpos(12) lwidth(thick)) 
			(connected COM_pct year, lcolor(red) mlabcolor(red) msym(none) mlabpos(12) lwidth(thick))
			(connected LIB_pct year, lcolor(dkorange) mcolor(dkorange) mlabcolor(dkorange)  msym(none) mlabpos(12) lwidth(thick))
			(connected CON_pct year, lcolor(navy) mcolor(navy) mcolor(navy) mlabcolor(navy) msym(none) mlabpos(12) lwidth(thick)) 
			(connected AGR_pct year, lcolor(blue) mlabcolor(blue) msym(none) lwidth(thick))
			(connected OTH_pct year, lcolor(gray) mlabcolor(gray) msym(none) mlabpos(12) lwidth(thick))
			(connected Fris_pct year, lcolor(orange) mlabcolor(orange) msym(none) mlabpos(12) lwidth(thick))
			, 
				ytitle("Party Vote Share", size(large)) ylab(0(10)60)  ysca(titlegap(*10)) 
				xtitle("Election Year", size(large)) xlab(1911 1913.8 " "  1914.2 "1914" 1917 1920 " " 1921 1924 1928 1932 1936 1940 1944)  
				xsca(titlegap(*10)) xline(1921, lwidth(5) lc(gs12)) 
				text(33 1928 "Conservative", color(navy)) 
				text(56 1940  "Social Dems", color(cranberry)) 
				text(47 1913 "Liberals", color(dkorange)) 
				text(5 1940 "Far Left", color(red)) 
				text(10 1914 "Agrarians", color(blue)) 
				text(15 1926 "Proh. Libs", color(orange))
				scheme(tufte) legend(off)  
		 ;
		# delimit cr
		
		 *Note: Other: 1920 includes Frisinnade and Federation of farmers. 1921 include Freedom Party, 1924 and 1928 include Frisinnade.

		
		 graph export "./images/Fig1a.eps", replace 

		 
		 
******************************************************
*3: Figure 1b: Distribution of Left Support Urban Versus Rural  
******************************************************
	use "Sweden 1911-1944_panel.dta", clear

		
	*rural

		sum left if year==1921 & red21==0 & left<=1 & City==0
		local lrmean=round(r(mean)*100, 0.01)
		
		# delimit ; 
		twoway 
			(kdensity left if year==1921 & red21==0 & left<=1, lcolor(navy) lwidth(vthick) )
			(kdensity  left if year==1920 & red21==0 & left<=1, lcolor(blue) lwidth(vthick)) 
			(kdensity  left if year==1917 & red21==0 & right<=1, lcolor(green) lwidth(vthick)) 
			if CityTown==0, 
			ytitle("") xtitle("")
			subtitle("Rural", size(vlarge)) 
			text(1.5 .8 "1921") text(1.84 .84 "1917") text(1.3 .30 "1920") 
			xline(`lrmean') 
			legend(off)  scheme(tufte) name(g1, replace) 
			;
		# delimit cr 


	*urban
		sum left if year==1921 & red21==0 & left<=1 & City==1
		local lumean=round(r(mean)*100, 0.01)
	
		# delimit ; 
		twoway 
			(kdensity left if year==1921 & red21==0 & left<=1, lcolor(navy) lwidth(vthick)) 
			(kdensity  left if year==1920 & red21==0 & left<=1, lcolor(blue) lwidth(vthick)) 
			(kdensity  left if year==1917 & red21==0 & right<=1, lcolor(green) lwidth(vthick)) 
			if CityTown==1, 
			ytitle("") xtitle("")
			subtitle("Urban", size(vlarge)) 
			text(.6 .95 "1921") text(1.82 .88 "1917") text(1.3 .38 "1920") 	
			xline(`lumean') 
			legend(off)  scheme(tufte) name(g2, replace) 
			;
		# delimit cr 

		graph combine g1 g2, ycommon l1title("kernel density", size(large)) b1title("Support for the Left (% valid votes)", size(large)) 


		graph export "./images/Fig1b.eps", replace



******************************************************
*4: Figure 1c: upport for the Left Based on Electorate Size, Urban and Rural  
******************************************************

	*Using the 1921 electorate size as the x-axis for comprability across parishes. 
	gen blah=Elect if year==1921
	egen elect1921=max(blah), by(ID)
	replace elect1921=ln(elect1921)
	drop blah
	
	forvalues n = 4(2)12 {
	local xlab `xlab' `n' "`=string(round(exp(`n'),10),"%9.0fc")'"
}
	

	# delimit ; 
	twoway 
		(scatter left elect1921 if City==0, mcolor(gs12) msym(o) msize(tiny)) 
		(scatter left elect1921  if City==1, mcolor(orange)) 
		(lowess left elect1921 if City==0, lcolor(gs9) lwidth(thick) lpattern(solid)) 
		(lowess left elect1921 if  City==1, lcolor(dkorange) lwidth(thick) lpattern(solid) ) 		if inlist(year, 1917, 1920, 1921 ), 
		by(year, legend(off) note("")  )  
		xtitle("Electorate 1921", size(large))
		xlab(`xlab') xsca(titlegap(*20))
		ytitle("Support for the Left (%)", size(large)) 
		scheme(tufte)  ;
	# delimit cr 
	*note("Rural in grey, Urban in orange, uses 1921 electorate size") title("S") 
	
	graph export "./images/Fig1c.eps", replace


***********************************************
*5: Figure 1d: Correlations across elections
***********************************************

	use "Sweden 1911-1944_panel.dta", clear

	*Left Lag and Lag2
		sum left if year<1921
		sort ID year
		
		by ID: gen L1=left[_n-1]
		by ID: gen L2=left[_n-2]
		
		la var L1 "Left previous election" 
		la var L2 "Left penultimate election" 

		 
	**Fig 1d(a)  Comparing 1914 to 1917**
		corr L1 L2 if L1<=1 & L2<=1 & year==1920

		# delimit ; 
		twoway 
			(scatter L1 L2 if L1<=1 & L2<=1 & year==1920 & CityTown==0, mcolor(gs12)) 
			(scatter L1 L2 if L1<=1 & L2<=1 & year==1920 & CityTown==1, mcolor(orange)) , 
			xtitle("Left Vote 1914", size(vlarge)) xsca(titlegap(*10)) 
			ytitle("Left Vote 1917", size(vlarge))  
			text(.08 .9 "corr=0.91", size(large)) 
			legend(off) name(g1, replace) scheme(tufte); 
			
		# delimit cr 



	**Fig 1d(b) Comparing 1917 to 1920**
		corr LEFT_pct L1 if L1<=1 & year==1920
		
		# delimit ; 
		twoway 
			(scatter left L1 if L1<=1 & year==1920 &  CityTown==0, mcolor(gs12)) 
			(scatter left L1 if L1<=1 & year==1920 & CityTown==1, mcolor(orange) ) , 
			xtitle("Left Vote 1917", size(vlarge)) xsca(titlegap(*10)) 
			ytitle("Left Vote 1920", size(vlarge)) 
			text(.08 .9 "corr=0.90", size(large)) 
			legend(off) name(g2, replace) scheme(tufte)
			;
			
		# delimit cr 

		graph combine g1 g2
		*b1title("Left Voting Correlation, Prior Elections", size(large))
		*NEED TO FIX THE AXES. WTF. Need to put the correlations inside. 

		graph export "./images/Fig1d.eps", replace



	*Sanity Check:look at distribution of change across election
		gen DiffLastleft=L1-L2 if L1<=1 & L2<=1 & year==1920

		twoway (scatter DiffLastleft L1 if year==1920) (scatter DiffLastleft L1 if year==1920 & DiffLastleft>.45 | DiffLastleft<-.45  , mlabel(ID) mlabpos(10) )
		*No strong correlation between the Support for the left in the last election and the change in support for the left. 
		 
